package com.zp.self.kafka;

import com.alibaba.fastjson.JSON;
import com.zp.self.kafka.entity.LogMsgEntity;
import com.zp.self.kafka.entity.MessageBody;
import com.zp.self.kafka.produce.KafkaProduceImpl;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

/**
 * @author By ZengPeng
 * @Description
 * @date in  2021/1/11 11:01
 * @Modified By
 */
@RestController
@RequestMapping("/kafka")
public class kafkaTestController {

    @Resource
    KafkaProduceImpl kafkaProduceImpl;

    @RequestMapping(value = "/send",method = RequestMethod.POST)
    public Object hi(String topic,@RequestBody List<LogMsgEntity> content) throws Exception {
        try {
            System.out.println(1/0);
        } catch (Exception e) {
            //创建消息
            ArrayList<LogMsgEntity> list = new ArrayList<>();
            LogMsgEntity a = new LogMsgEntity();
            a.setData(e.getMessage()+e.toString());
            a.setId("123");
            a.setDate("2020-08-04 16:40:20.123");
            a.setServiceName("command");
            list.add(a);

            //创建消息发送
            MessageBody<ArrayList<LogMsgEntity>> messageBody = new MessageBody<>();
            messageBody.setData(list);
            kafkaProduceImpl.sendAsyncMsg(topic,JSON.toJSONString(messageBody));
        }

        return "Ok!"+content;
    }
}
